//
//  DJAttentionViewController.m
//  Adorable_Things
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 Banana Inc. All rights reserved.
//

#import "DJAttentionViewController.h"
#import "DJCommentModel.h"
#import "DJAttentionCollectionViewCell.h"
#import "AttentionHeaderCollectionReusableView.h"
#import "ATMainDetailViewController.h"

#define kAttentionUrl @"http://api.menghuoapp.com/v1/shop/followed"

@interface DJAttentionViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) NSMutableArray *attentionArr;
@property (nonatomic, strong) UICollectionView *collectionV;
@property (nonatomic, assign) NSInteger page;

@end

@implementation DJAttentionViewController

- (void)loadView
{
    [super loadView];
    self.view.frame = CGRectMake(0, 50, kWidth, kHeight - 99);

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self addsubView];
    [self setUpdata];
    
    self.attentionArr = [NSMutableArray array];
    
    self.collectionV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 0;
        [self setUpdata];
    }];
    
    self.collectionV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page += 1;
        [self setUpdata];
    }];
}

- (void)setUpdata
{
    ATUserManager *userManager = [ATUserManager shareManager];
    
   NSDictionary *parDic = @{@"page_size":@"20",@"page":[NSString stringWithFormat:@"%ld",(long)self.page],@"auth_token":userManager.auth_token};
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:kAttentionUrl parameters:parDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (self.page == 0) {
            [self.attentionArr removeAllObjects];
        }
        
        NSArray *dataArr = responseObject[@"data"];
        for (NSDictionary *oneDic in dataArr) {
            DJCommentModel *model = [[DJCommentModel alloc]init];
            [model setValuesForKeysWithDictionary:oneDic];
            [self.attentionArr addObject:model];
        }
        [self.collectionV.mj_header endRefreshing];
        [self.collectionV.mj_footer endRefreshing];
        [self.collectionV reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

- (void)addsubView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.headerReferenceSize = CGSizeMake(100, 26);
    layout.itemSize = CGSizeMake( (kWidth - 30 ) / 2, 250);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, self.view.height) collectionViewLayout:layout];
    self.collectionV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionV];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    
    [self.collectionV registerNib:[UINib nibWithNibName:@"DJAttentionCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"AttentionCollectionCell"];
    
    [self.collectionV registerNib:[UINib nibWithNibName:@"AttentionHeaderCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"AttentionHeader"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    DJCommentModel *model = self.attentionArr[section];
    return model.items.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.attentionArr.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DJAttentionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AttentionCollectionCell" forIndexPath:indexPath];
    DJCommentModel *model = self.attentionArr[indexPath.section];
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.items[indexPath.row][@"pic_url"]]];
    cell.imageV.layer.cornerRadius = 5;
    cell.imageV.layer.masksToBounds = YES;
    cell.nameLab.text = model.items[indexPath.row][@"title"];
    cell.priceLab.text = [NSString stringWithFormat:@"￥%@", model.items[indexPath.row][@"price"]];
    cell.Lab.text = [NSString stringWithFormat:@"人气%@",model.items[indexPath.row][@"favorite_num"]];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    AttentionHeaderCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"AttentionHeader" forIndexPath:indexPath];
    DJCommentModel *model = self.attentionArr[indexPath.section];
    [headView.imageV sd_setImageWithURL:[NSURL URLWithString:model.logo]];
    headView.imageV.layer.cornerRadius = 10;
    headView.imageV.layer.masksToBounds = YES;
    headView.nameLab.text = model.name;
    [headView.countLab setTitle:[NSString stringWithFormat:@"%lu",(unsigned long)model.items.count] forState:(UIControlStateNormal)];
    headView.countLab.layer.cornerRadius = 10;
    headView.countLab.layer.masksToBounds = YES;
    
    return headView;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // cell点击方法
    DJCommentModel *model = _attentionArr[indexPath.section];
    ATMainDetailViewController *mainVC = [[ATMainDetailViewController alloc]init];
    mainVC.item_id = model.items[indexPath.row][@"item_id"];
    mainVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:mainVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
